package com.totcy.atom.view.fragment;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.animation.DecelerateInterpolator;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.totcy.atom.R;
import com.totcy.atom.adapter.PaperListAdapter;
import com.totcy.atom.bean.PaperBean;
import com.totcy.atom.presenter.PaperPresenter;
import com.totcy.atom.presenter.contract.PaperContract;
import com.totcy.atom.view.activity.QuestionActivity;
import com.totcy.baselibrary.adapter.BaseLoadMoreAdapter;
import com.totcy.baselibrary.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class PaperListFragment extends BaseFragment<PaperContract.View, PaperPresenter> implements BaseLoadMoreAdapter.OnItemClickListener, OnRefreshListener, PaperContract.View {
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.refresh)
    SmartRefreshLayout refresh;

    private PaperListAdapter paperListAdapter;
    private List<PaperBean> list = new ArrayList<>();
    private String subjectName;

    public PaperListFragment setSubjectName(String subjectName) {
        this.subjectName = subjectName;
        return this;
    }

    @Override
    public int getLayoutId() {
        return R.layout.layout_refresh_list;
    }

    @Override
    public void initView(Bundle savedInstanceState) {

        LinearLayoutManager lm = new LinearLayoutManager(getActivity());
        lm.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(lm);
        paperListAdapter = new PaperListAdapter(getActivity(), list);
        recyclerView.setAdapter(paperListAdapter);
        paperListAdapter.setOnItemClickListener(this);
        //设置回弹动画插值器 减速效果
        refresh.setReboundInterpolator(new DecelerateInterpolator());
        refresh.setOnRefreshListener(this);
        //自动刷新
        refresh.autoRefresh(200);

    }

    @Override
    public void onItemClick(int position) {
        Bundle bundle = new Bundle();
        bundle.putString("date", list.get(position).getDate());
        bundle.putString("subject", list.get(position).getSubject().getName());
        bundle.putInt("type", getType(getFragmentTitle()));
        openActivity(QuestionActivity.class, bundle);
    }

    public void refresh(){
        //自动刷新
        refresh.autoRefresh(200);
    }

    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        getPresenter().getPaperList(subjectName, getType(getFragmentTitle()));
    }

    @Override
    public void onPaperList(List<PaperBean> papers) {
        refresh.finishRefresh(true);
        list.clear();
        list.addAll(papers);
        paperListAdapter.notifyDataSetChanged();
    }

    @Override
    public void onFail(int code, String message) {
        refresh.finishRefresh(false);
        ToastUtil.showToast(getActivity(), message);
    }

    private int getType(String title) {
        if ("综合".equals(title)) {
            return 1;
        } else if ("案例".equals(title)) {
            return 2;
        } else {
            return 3;
        }
    }
}
